package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.CheckDishException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.exception.SetmealExistException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;

    private final SetmealDishMapper setmealDishMapper;

    private final DishMapper dishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);
        List<Setmeal> setmealList = setmealMapper.selectSetMealList(setmeal);
        PageInfo<Setmeal> pageInfo = new PageInfo(setmealList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    @Transactional
//    @CacheEvict(cacheNames = "SETMEAL_BY_CATEGORYID" , key = "setmealDTO.categoryId")
    public void add(SetmealDTO setmealDTO) {
        Integer countName = setmealMapper.selectSetmealByName(setmealDTO.getName());
        if (countName > 0){
            throw new SetmealExistException(MessageConstant.REPEAT_SETMEAL_NAME);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insertSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO rowInfoById(Long id) {
        SetmealVO setmealVO = setmealMapper.rowInfoById(id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectById(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    @Transactional
    @Override
    @CacheEvict(cacheNames = "SETMEAL_BY_CATEGORYID" , key = "setmealDTO.categoryId")
    public void update(SetmealDTO setmealDTO) {
        Integer countName = setmealMapper.selectSetmealByNameNotSelf(setmealDTO.getId(), setmealDTO.getName());
        if (countName > 0){
            throw new SetmealExistException(MessageConstant.REPEAT_SETMEAL_NAME);
        }
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Integer disEnableStatusCount = dishMapper.selectDisEnableStatusCountByDishId(setmealDishes);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        if (disEnableStatusCount > 0){
            setmeal.setStatus(StatusConstant.DISABLE);
        }
        setmealDishMapper.deleteSetmealDishByIds(Collections.singletonList(setmealDTO.getId()));
        createBatchSetmealDish(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        Integer countByIdsAndStatus = setmealMapper.selectCountByIdsAndStatus(ids);
        if(countByIdsAndStatus > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteSetmealDishByIds(ids);
    }

    @Override
    @CacheEvict(cacheNames = "SETMEAL_BY_CATEGORYID" , allEntries = true)
    public void changeStatus(Long id , Integer status) {
        List<SetmealDish> setmealDishList = setmealDishMapper.selectBySetmealId(id);
        setmealDishList.forEach(setmealDish -> {
            Integer countDishDisable = dishMapper.selectDishDisableById(setmealDish.getDishId());
            if (countDishDisable > 0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    @Cacheable(cacheNames = "SETMEAL_BY_CATEGORYID" , key = "#categoryId")
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectSetmealByCategoryId(categoryId);
        return setmealList;
    }

    @Override
    public List<DishItemVO> selectDishItemById(Long id) {
        List<DishItemVO> dishItemVOList = dishMapper.selectDishItemById(id);
        return dishItemVOList;
    }

    private void createBatchSetmealDish(SetmealDTO setmealDTO, Setmeal setmeal) {
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }
}
